package com.team7.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.team7.common.entity.CheckTeacher;
import com.team7.common.entity.Teacher;
import com.team7.common.vo.AddTeacherVo;
import com.team7.common.vo.CommonResult;
import com.team7.common.vo.TeacherCheckVo;
import com.team7.common.vo.TeacherVo;
import com.team7.dao.CheckTeacherDao;
import com.team7.dao.TeacherDao;
import com.team7.service.TeacherService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @program: team7-Object02
 * @description: 讲师service接口实现类
 * @author: 王子鑫
 * @create: 2021-10-18 22:43
 **/
@Service
public class TeacherServiceImpl implements TeacherService {
    @Resource
    private TeacherDao teacherDao;

    @Resource
    private CheckTeacherDao checkTeacherDao;

    //查询所有
    @Override
    public CommonResult findAllTeacher(Integer page, Integer limit, TeacherVo teacherVo) {
        Page<TeacherVo> teacherPage = new Page<>(page,limit);
        QueryWrapper<TeacherVo> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(teacherVo.getName())){
            wrapper.like("name",teacherVo.getName());
        }
        if (StringUtils.isNotEmpty(teacherVo.getMobile())){
            wrapper.like("mobile",teacherVo.getMobile());
        }
        if (StringUtils.isNotEmpty(teacherVo.getStatus())){
            wrapper.like("status",teacherVo.getStatus());
        }
        Page<TeacherVo> allTeacher = teacherDao.findAllTeacher(teacherPage, wrapper);
        return new CommonResult(200,"查询成功",allTeacher);
    }

    //修改状态
    @Override
    public CommonResult changeStatus(TeacherVo teacherVo) {
        int i = teacherDao.changeStatus(teacherVo.getStatus(),teacherVo.getId());
        if (i>0){
            return new CommonResult(200,"修改状态成功","success");
        }else {
            return new CommonResult(500,"修改状态失败","default");
        }
    }

    //修改信息
    @Override
    public CommonResult updateTeacherInfo(TeacherVo teacherVo) {
        int i = teacherDao.updateTeacherInfo(teacherVo);
        if (i>0){
            List<TeacherVo> teacherByName = teacherDao.getTeacherByName(teacherVo);
            if (teacherByName.size()>0){
                int i1 = teacherDao.updateTeacherName(teacherVo);
                if (i1>0){
                    return new CommonResult(200,"修改信息成功","success");
                }else {
                    return new CommonResult(500,"修改信息失败","default");
                }
            }
            return new CommonResult(200,"修改信息成功","success");
        }else{
            return new CommonResult(500,"修改信息失败","default");
        }
    }

    //新增讲师
    @Override
    public CommonResult addTeacher(AddTeacherVo addTeacherVo) {
        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        Teacher teacher1 = new Teacher();
        teacherQueryWrapper.eq("mobile",addTeacherVo.getMobile());
        Teacher teacher = teacherDao.selectOne(teacherQueryWrapper);
        if (teacher!=null){
            return new CommonResult(500,"该电话号码已经存在","default");
        }else {
            QueryWrapper<Teacher> teacherQueryWrapper1 = new QueryWrapper<>();
            teacher1.setName(addTeacherVo.getName());
            teacher1.setIntro(addTeacherVo.getIntro());
            teacher1.setEmail(addTeacherVo.getEmail());
            teacher1.setGmtCreate(LocalDateTime.now().toString());
            teacher1.setAvatar(addTeacherVo.getAvatar());
            teacher1.setLevel(addTeacherVo.getLevel());
            teacher1.setGmtModified(LocalDateTime.now().toString());
            teacher1.setMobile(addTeacherVo.getMobile());
            teacher1.setIsDeleted(false);
            teacher1.setStatus("0");
            teacher1.setSort(0);
            int i = teacherDao.insert(teacher1);
            teacherQueryWrapper1.eq("mobile",addTeacherVo.getMobile());
            Teacher teacher2 = teacherDao.selectOne(teacherQueryWrapper1);
            CheckTeacher checkTeacher = new CheckTeacher();
            checkTeacher.setTeacherId(teacher2.getId());
            checkTeacher.setCheckStatus("2");
            int i1 = checkTeacherDao.insert(checkTeacher);
            if (i>0&&i1>0){
                return new CommonResult(200,"新增讲师成功","success");
            }else {
                return new CommonResult(500,"新增讲师失败","default");
            }
        }

    }
}
